package com.study.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.services.OpportunityService;

@Controller
public class OpportunityController {
	@Autowired
	OpportunityService os;
	/**
	 * Wcx
	 * 
	 * @param dname
	 */
	@GetMapping("addopport")
	public ModelAndView addopp() {
		System.out.println("新建进来了");
		ModelAndView mv = new ModelAndView("beforeSale/opportadd");
		JSONObject jsonObject = new JSONObject();
		mv.addObject("addopports", jsonObject);
		return mv;
	}
	
	@GetMapping("opport")
	public ModelAndView deptAdd() {
		// @GetMapping没有带斜杠/，默认的视图是jsp/add.jsp页面
		ModelAndView mv = new ModelAndView("beforeSale/opport");
		System.out.println("来了老弟");
		return mv;
	}


	@PostMapping("addopportunity")
	@ResponseBody
	public ModelAndView aa(String theme,String custid,String state,String type,String linid,String laiyuan,
			               String demand,String possbity,String exprice,String youxian,String stage,
			               Date exdate ,HttpServletRequest request) {
		System.out.println("我来了Post");
		//获取当前所登陆的员工
				String useid= request.getSession().getAttribute("useid").toString();
		System.out.println( theme+"  "+ custid+"  "+ state+"  "+ type+"  "+linid+"  "+laiyuan+"  "+
	                 demand+"  "+ possbity+"  "+ exprice+"  "+ youxian+"  "+ stage+"  "+ exdate);
		boolean result = os.insertopp( theme, custid, state, type, linid, laiyuan, useid, 
				                       demand, possbity, exprice, youxian, stage,exdate,new Date());
		ModelAndView mv = new ModelAndView("/beforeSale/opport");
		return mv;
	}
	
	@PostMapping("updateopportunity")
	@ResponseBody
	public ModelAndView bb(String oppid,String theme,String custname,String state,String type,String linkman,String laiyuan,
			               String demand,String possbity,String exprice,String youxian,String stage,String custid,String linid,
			               Date exdate,HttpServletRequest request ) {
		
		String useid= request.getSession().getAttribute("useid").toString();
		boolean result = os.updateopp( oppid,theme, custname, state, type, linkman, laiyuan,
				                       demand, possbity, exprice, youxian, stage,exdate,useid, custid,linid);
		ModelAndView mv = new ModelAndView("/beforeSale/opport");
		return mv;
	}
	
	/**
	 * 带条件的分页查询
	 * 
	 * @param input
	 * @param selects
	 * @param currentPage
	 * @param pagesize
	 * @param session
	 * @param request
	 * @return
	 */
	@PostMapping("beforeSale/TJopport")
	@ResponseBody
	public String  getForm3(String input, String selects, int currentPage, int pagesize, HttpSession session,
			HttpServletRequest request) {
		
		Integer cusid = (Integer) session.getAttribute("cusview");
		//获取当前所登陆的员工
		String useid= request.getSession().getAttribute("useid").toString();
		List<JSONObject> list = os.selectBySel(currentPage, pagesize, input, selects,useid,cusid);
		
		System.out.println(list.size() + "长度");
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
        System.out.println(json);
        session.removeAttribute("cusview");
		return json;
	}
	
	
	/**
	 * 修改进来了
	 * @param linid
	 * @return
	 */
	@GetMapping("updateopport")
	public ModelAndView updatelinkman(String id){
		System.out.println("进来了");
		System.out.println(id);
		ModelAndView mv = new ModelAndView("beforeSale/opportadd");
		System.out.println("修改" +id +" 进来了");
		JSONObject jsonObject = os.selectByid(id);
		System.out.println(jsonObject);
		mv.addObject("addopports",jsonObject);
		System.out.println("323");
		return mv;
	}
	
	
	
	@GetMapping("/opportview")
	public ModelAndView  opportView(HttpServletRequest request) {
		String id = request.getParameter("id");
		// @GetMapping没有带斜杠/，默认的视图是jsp/add.jsp页面
		ModelAndView mv = new ModelAndView("beforeSale/opportView");
		//找到最新的一系列记录
		List<JSONObject> js = os.selectView(id);
		//查找个个阶段的数量
		JSONObject count= os.selectCount(id);
		System.out.println(js);
		System.out.println("-----------------------------------------------------------");
		mv.addObject("oppView", js);
		mv.addObject("oppCount", count);
		return mv;
	}

}
